﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;
using System.Windows.Forms;
using DataLoad.iContact.WinClient.Properties;

namespace DataLoad.iContact.WinClient
{
    public class ExLabelControl : Label
    {
        public ExLabelControl() : base()
        {
            TextAlign = ContentAlignment.MiddleCenter;
        }

        #region Gets the desired Capsular path.
        private GraphicsPath GetCapsule(RectangleF baseRect)
        {
            float diameter;
            RectangleF arc;
            GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
            try
            {
                if (baseRect.Width > baseRect.Height)
                {
                    // return horizontal capsule 

                    diameter = baseRect.Height;
                    SizeF sizeF = new SizeF(diameter, diameter);
                    arc = new RectangleF(baseRect.Location, sizeF);
                    path.AddArc(arc, 90, 180);
                    arc.X = baseRect.Right - diameter;
                    path.AddArc(arc, 270, 180);
                }
                else if (baseRect.Width < baseRect.Height)
                {
                    // return vertical capsule 

                    diameter = baseRect.Width;
                    SizeF sizeF = new SizeF(diameter, diameter);
                    arc = new RectangleF(baseRect.Location, sizeF);
                    path.AddArc(arc, 180, 180);
                    arc.Y = baseRect.Bottom - diameter;
                    path.AddArc(arc, 0, 180);
                }
                else
                {
                    // return circle 

                    path.AddEllipse(baseRect);
                }
            }
            catch (Exception)
            {
                path.AddEllipse(baseRect);
            }
            finally
            {
                path.CloseFigure();
            }
            return path;
        }
        #endregion

        private Color _roundColor = Color.DarkGray;

        public Color RoundColor 
        {
            get
            {
                return _roundColor;
            }
            set
            {
                _roundColor = value;
            }
        }

        private bool _inEditMode = false;

        [DefaultValue(false)]
        public bool InEditMode
        {
            get
            {
                return _inEditMode;
            }

            set
            {
                _inEditMode = value;
            }
        }

        private string[] _dropDownItems;

        public string[] DropDownItems
        {
            get
            {
                return _dropDownItems;
            }
            set
            {
                _dropDownItems = value;
            }
        }

        public ContextMenuStrip ClickMenu { get; set; }

        private bool _active = false;

        private bool _displayDropDown = false;

        [DefaultValue(false)]
        public bool DisplayDropDown
        {
            get
            {
                return _displayDropDown;
            }

            set
            {
                _displayDropDown = value;
                Invalidate();
            }
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            if ((_active) && (!_inEditMode))
            {
                // Draw the background
                Rectangle re = new Rectangle(0, 0, Width - 1, Height - 1);
                e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
                e.Graphics.FillPath(new SolidBrush(_roundColor), GetCapsule(re));

                if (_displayDropDown)
                {
                    e.Graphics.DrawImage(Resources.Arrow, Width - 12, (Height - Resources.Arrow.Height) / 2);
                }
            }

            base.OnPaint(e);
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);

            if (!_inEditMode)
            {
                _active = true;
                Invalidate();
            }
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);

            if (!_inEditMode)
            {
                _active = false;
                Invalidate();
            }
        }

        protected override void OnClick(EventArgs e)
        {
            if (!_inEditMode)
            {
                base.OnClick(e);

                if (ClickMenu != null)
                {
                    ClickMenu.Show(this, 0, Height);
                }
            }
        }
    }
}
